AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include( "shared.lua" )

function ENT:Initialize()
	if self.Model == nil then
		print( self, " does not have a valid model" )
		self:Remove()
		return		
	end

	self:SetModel(self.Model)
	
	self:PhysicsInit(SOLID_VPHYSICS)
	self:SetMoveType(MOVETYPE_VPHYSICS)
	self:SetSolid(SOLID_VPHYSICS)
	self:SetUseType(SIMPLE_USE)
	self:DrawShadow(false)
	
	local phys = self:GetPhysicsObject()
	if (phys:IsValid()) then
	
		phys:SetMass( 100 )
		phys:Wake()
		phys:EnableGravity( true )
		
	end
	
	self:SetAllowMotion( true )
	self:ResetLife()

end

function ENT:ResetLife()
	self:SetLife( Block.MaxHealth() )
end

function ENT:DoDamange( damage )
	
	local NewLife = self:Life() - damage
	
	if NewLife <= 0 then
		timer.Simple( 0, self.Remove, self )
		return
	end
	
	if self:AllowMotion() == false && NewLife <= Block.TrashHold() then
		self:SetAllowMotion( true )
	end
	
	self:SetLife( NewLife )
	
end

function ENT:Team()
	return self._LastTouchTeam or self._Team
end

function ENT:UpdateSkin( ply )

	if ply then
		self._LastTouch = ply
		self._LastTouchTeam = ply:Team()
	end
	
	if ValidPlayer( self._LastTouch ) then
	
		local TeamId = self._LastTouch:Team()
		local SkinId = math.Clamp( math.floor( (1-self:HealthFrac()) * 6), 0, 5 )
		
		if TeamId == Team.BLUE then
			SkinId = SkinId + 6
		end
		
		self:SetSkin( SkinId )
		
	end
	
end

function ENT:SetSpawner( ply )
	
	if !ValidPlayer( ply ) then
		return
	end
	
	self.Player = ply
	self._Team = ply:Team() 
	
	self:UpdateSkin( self.Player )

end 

function ENT:AllowFreeze()

	for _, ent in pairs( ents.FindInBox( self:WorldSpaceAABB() ) ) do
		
		if ent:GetClass() == "func_nofreeze" then
			return false
		end
		
	end
	
	return true

end

hook.Add("BlockFreeze", "CheckFreeZone", function( block )

	if block.AllowFreeze && block:AllowFreeze() == false then
		return false
	end

end )

/*
concommand.Add("nc_check_collissions", function( ply, cmd, args )

	for _, ent in pairs( ents.FindByClass("func_nofreeze" ) ) do
	
		for _, block in pairs( ents.FindByClass("sf_block_*" ) ) do
		
			local res = util.TraceEntityHull(ent , block  )
			
			print( tostring(block) )
			
			for k, v in pairs( res ) do
				
				print( "\t" .. tostring(k) .. "\t" .. tostring(v) )
				
			end
			
		end
	
	end	

end )

concommand.Add("nc_check_collissions2", function( ply, cmd, args )

	for _, block in pairs( ents.FindByClass("sf_block_*" ) ) do
		
		local mins, maxs = block:WorldSpaceAABB()
		local Pos = block:GetPos()
		local up = block:GetUp()

		mins, maxs = (mins - Pos) * 0.95, (maxs - Pos) * 0.95
		OrderVectors(mins, maxs)

		local trace = util.TraceHull( {
			mins = mins,
			maxs = maxs,
			start = Pos,
			endpos = Pos,
			filter = ents.FindByClass("sf_block_*"),
			mask = MASK_ALL
		} )
		
		print( tostring(block) )
			
		for k, v in pairs( trace ) do
			print( "\t" .. tostring(k) .. "\t" .. tostring(v) )
		end
		
		
	end

end )


concommand.Add("nc_check_collissions3", function( ply, cmd, args )

	for _, block in pairs( ents.FindByClass("sf_block_*" ) ) do
		
		local mins, maxs = block:WorldSpaceAABB()
		local trace = util.TraceEntity( {
			start = mins,
			endpos = maxs,
			mask = MASK_ALL
		}, block )
		
		print( tostring(block) )
			
		for k, v in pairs( trace ) do
			print( "\t" .. tostring(k) .. "\t" .. tostring(v) )
		end
		
	end

end )

concommand.Add("nc_check_collissions4", function( ply, cmd, args )

	for _, block in pairs( ents.FindByClass("sf_block_*" ) ) do
		
		print( block )
		
		for _, ent in pairs( ents.FindInBox( block:WorldSpaceAABB() ) ) do
			
			print( "\t" .. tostring(ent) )
		
		end
		
	end

end )
*/